package com.algorithm.example.linkedList;

import java.util.Iterator;

/**
 * <p>
 * 双向哨兵链表
 * </p>
 *
 * @author lzt
 * @version 1.0
 * @since 2024/1/19 11:55
 */
public class DoublyLinkedListSentinel implements Iterable<Integer> {

    /**
     * 头哨兵
     */
    private Node head;

    /**
     * 尾哨兵
     */
    private Node tail;

    private static class Node {
        /**
         * 上一个节点指针
         */
        Node prev;

        /**
         * 值
         */
        int value;

        /**
         * 下一个节点
         */
        Node next;

        public Node(Node prev, int value, Node next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }
    }

    public DoublyLinkedListSentinel() {
        this.head = new Node(null, 666, null);
        this.tail = new Node(null, 111, null);
        this.head.next = tail;
        this.tail.prev = head;
    }

    /**
     * 添加
     */
    public void addFirst(int value) {
        Node node = new Node(head, value, head.next);
        head.next.prev = node;
        head.next = node;
    }


    /**
     * 迭代器遍历
     */
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node p = head.next;

            @Override
            public boolean hasNext() {
                return p != tail;
            }

            @Override
            public Integer next() {
                int value = p.value;
                p = p.next;
                return value;
            }
        };
    }

    /**
     * 根据索引查询，返回元素值
     */
    public int get(int value) {
        Node node = findNode(value);
        if (node == null) {
            throw new RuntimeException("出错了");
        }
        return node.value;
    }

    /**
     * 根据索引插入
     */
    public void insert(int index, int value) {
        Node node = findNode(index);
        if(node == null) {
            throw new RuntimeException("出错");
        }
        Node prev = node.prev;
        Node insertNode = new Node(prev, value, node);
        prev.next = insertNode;
        node.prev = insertNode;

    }

    /**
     * 根据索引值删除元素
     */
    public void remove(int value) {
        Node node = findNode(value);
        if(node == null) {
            throw new RuntimeException("出错了");
        }
        Node prev = node.prev;
        node.next.prev = prev;
        prev.next = node.next;

    }

    /**
     * 删除第一个元素
     */
    public void removeFirst() {
        remove(0);
    }

    /**
     * 根据索引返回Node
     */
    private Node findNode(int value) {
        int i = -1;
        Node p = head;
        while (p != tail) {
            if (i == value) {
                return p;
            }
            i++;
            p = p.next;
        }
        return null;
    }


    /**
     * 添加元素到最后
     */
    public void addLast(int value) {
        Node prev = tail.prev;
        Node Lastnode = new Node(prev, value, tail);
        prev.next = Lastnode;
        tail.prev = Lastnode;
    }
}
